package io.gitee.devaper.starlight.security.handler

import io.gitee.devaper.starlight.common.entity.vo.*
import io.gitee.devaper.starlight.common.util.*
import io.gitee.devaper.starlight.security.event.*
import jakarta.servlet.http.*
import org.springframework.context.ApplicationEventPublisher
import org.springframework.context.ApplicationEventPublisherAware
import org.springframework.security.core.*
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler
import org.springframework.stereotype.Component

/**
 * 登出成功处理器
 */
@Component
open class DefaultLogoutSuccessHandler : LogoutSuccessHandler, ApplicationEventPublisherAware {
    override fun onLogoutSuccess(
        request: HttpServletRequest,
        response: HttpServletResponse,
        authentication: Authentication
    ) {
        applicationEventPublisher.publishEvent(InteractiveLogoutEvent(authentication.principal))
        response.write(data = ApiResult.success(), request = request)
    }

    private lateinit var applicationEventPublisher: ApplicationEventPublisher

    override fun setApplicationEventPublisher(applicationEventPublisher: ApplicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher
    }
}